package com.example.test622.ui.cinema

import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import com.example.common.BR
import com.example.common.CommonAdapter
import com.example.common.CommonViewHolder
import com.example.common.mvi.BaseFragment
import com.example.test622.R
import com.example.net.entity.MutilTypeEntity
import com.example.net.entity.MutilVideoEntity
import com.example.net.event.Event
import com.example.test622.databinding.CinemaaBinding
import com.example.test622.databinding.FragmentDashboardBinding
import com.google.android.material.tabs.TabLayout
import kotlinx.coroutines.launch

class CinemaFragment : BaseFragment<FragmentDashboardBinding, CinemaViewModel>() {
	var isL = true
	private var page: Int = 0
	private var pagesize: Int = 20
	private var typeId = 0
	val adapter by lazy {
		CommonAdapter<MutilTypeEntity>({ R.layout.cinema to BR.cinema })
	}
	val v_adapter by lazy {
		object : CommonAdapter<MutilVideoEntity>({ R.layout.cinemaa to BR.svideo }) {
			override fun onBindViewHolder(holder: CommonViewHolder, position: Int) {
				super.onBindViewHolder(holder, position)
				val binding = holder.binding as CinemaaBinding
				var flag = true
				binding.sc.setOnClickListener {
					if (flag) {
						flag = false
//						val drawable = resources.getDrawable(R.drawable.ic_home_detail_description_collect_check)
//						drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
						Glide.with(requireContext()).load(R.drawable.ic_stare).into(binding.sc)
						Toast.makeText(requireContext(), "收藏成功", Toast.LENGTH_SHORT).show()
						
					} else {
						flag = true
//						val drawable = resources.getDrawable(R.drawable.ic_home_detail_description_collect)
//						drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
						Glide.with(requireContext()).load(R.drawable.ic_star).into(binding.sc)
						Toast.makeText(requireContext(), "取消收藏", Toast.LENGTH_SHORT).show()
					}
				}
			}
		}
	}
	
	
	override fun initView() {
		binding.recCinema.adapter = adapter
		binding.videoCinema.adapter = v_adapter
		binding.tabCinema.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
			override fun onTabSelected(tab: TabLayout.Tab?) {
				if (tab?.tag != null) {
					adapter.clear()
					val entity = tab.tag as MutilTypeEntity
					viewModel.intent.trySend(CinemaIntent.LoadMutilBypid(entity.id))
				}
			}
			
			override fun onTabUnselected(tab: TabLayout.Tab?) {
			
			}
			
			override fun onTabReselected(tab: TabLayout.Tab?) {
			
			}
		})
	}
	
	override fun initData() {
		if (isL) {
			lifecycleScope.launch {
				viewModel.state.observe(requireActivity()) {
					when (it) {
						is CinemaState.getMutiltype -> {
							if (it.mutil.data.isNotEmpty()) {
								for (item in it.mutil.data) {
									binding.tabCinema.addTab(
										binding.tabCinema.newTab().setText(item.typename)
											.setTag(item)
									)
								}
							}
						}
						
						is CinemaState.getMutilVideo -> {
							v_adapter += it.video.data
							typeId = it.video.data[0].id
						}
						
						is CinemaState.getMutiltypeByPid -> {
							adapter += it.mPid.data
						}
						
						is CinemaState.failue -> {
							Toast.makeText(requireContext(), "${it.msg}", Toast.LENGTH_SHORT).show()
						}
					}
				}
				viewModel.intent.trySend(CinemaIntent.LoadMutil)
//				viewModel.intent.trySend(CinemaIntent.LoadMutilBypid(1))
				viewModel.intent.trySend(CinemaIntent.LoadMutilVideo(0, 10, 20))
				
				isL = false
			}
		}
	}
	
	@Event("GetId")
	fun GetId(id: Int) {
		typeId = id
		if (typeId != 0) {
			v_adapter.clear()
			viewModel.intent.trySend(CinemaIntent.LoadMutilVideo(page, pagesize, typeId))
		}
	}
}